package com.and.video.service;

import com.and.video.dao.UserFollowingDao;
import com.and.video.domain.FollowingGroup;
import com.and.video.domain.User;
import com.and.video.domain.UserFollowing;
import com.and.video.domain.UserInfo;
import com.and.video.domain.constant.UserConstant;
import com.and.video.domain.exception.ConditionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;


@Service
public class UserFollowingService{

    @Autowired
    private UserFollowingDao userFollowingDao;

    @Autowired
    private FollowingGroupService followingGroupService;

    @Autowired
    private UserService userService;

    // 新增用户关注
    @Transactional
    public void addUserFollowings(UserFollowing userFollowing) {
        // 获取分组id
        Long groupId = userFollowing.getGroupId();
        // id为空, 设置默认分组
        // id不为空, 获取分组对象
        if (groupId == null) {
            FollowingGroup followingGroup = followingGroupService.getByType(UserConstant.USER_FOLLOWING_GROUP_TYPE_DEFAULT);
            userFollowing.setGroupId(followingGroup.getId());
        } else {
            FollowingGroup followingGroup = followingGroupService.getById(groupId);
            if (followingGroup == null) {
                throw new ConditionException("关注分组不存在！");
            }
        }
        // 获取关注用户id
        Long followingId = userFollowing.getFollowingId();
        // 获取关注用户
        User user = userService.getUserById(followingId);
        if (user == null) {
            throw new ConditionException("关注的用户不存在！");
        }
        // 新增和更新都使用这套逻辑, 先删除用户和关注用户的关系, 再新增用户和关注用户的关系
        userFollowingDao.deleteUserFollowing(userFollowing.getUserId(), followingId);
        userFollowing.setCreateTime(new Date());
        userFollowingDao.addUserFollowing(userFollowing);
    }

    // 第一步：获取关注的用户列表
    // 第二步：根据关注用户的id查询关注用户的基本信息
    // 第三步：将关注用户按关注分组进行分类
    public List<FollowingGroup> getUserFollowings(Long userId) {
        // 根据用户id, 获取关注列表及关注用户所对应的关注分组
        List<UserFollowing> list = userFollowingDao.getUserFollowings(userId);

        // 从list中获取关注用户的id, 得出集合
        Set<Long> followingIdSet = list.stream().map(UserFollowing::getFollowingId).collect(Collectors.toSet());
        List<UserInfo> userInfoList = new ArrayList<>();
        if(followingIdSet.size() > 0){
            // 根据关注用户的ids来获取到用户具体信息集合
            userInfoList = userService.getUserInfoByUserIds(followingIdSet);
        }

        for(UserFollowing userFollowing : list){
            for(UserInfo userInfo : userInfoList){
                if(userFollowing.getFollowingId().equals(userInfo.getUserId())){
                    // 遍历关注用户的集合
                    // 设置用户详细信息
                    userFollowing.setUserInfo(userInfo);
                }
            }
        }

        // 获取关注的分组
        List<FollowingGroup> groupList = followingGroupService.getByUserId(userId);
        // 设置全部关注
        FollowingGroup allGroup = new FollowingGroup();
        // 把所有的关注用户信息放入
        allGroup.setName(UserConstant.USER_FOLLOWING_GROUP_ALL_NAME);
        allGroup.setFollowingUserInfoList(userInfoList);

        List<FollowingGroup> result = new ArrayList<>();
        result.add(allGroup);
        // 根据关注分组类型来分别放入
        for(FollowingGroup group : groupList){
            // 某个分组类型的集合
            List<UserInfo> infoList = new ArrayList<>();
            for(UserFollowing userFollowing : list){
                if(group.getId().equals(userFollowing.getGroupId())){
                    infoList.add(userFollowing.getUserInfo());
                }

            }
            group.setFollowingUserInfoList(infoList);
            result.add(group);
        }
        return result;
    }

    // 第一步：获取当前用户的粉丝列表
    // 第二步：根据粉丝的用户id查询基本信息
    // 第三步：查询当前用户是否已经关注该粉丝
    public List<UserFollowing> getUserFans(Long userId){
        // 获取关注id是userId的用户集合, 也就是粉丝集合
        List<UserFollowing> fanList = userFollowingDao.getUserFans(userId);
        // 获取粉丝id集合
        Set<Long> fanIdSet = fanList.stream().map(UserFollowing::getUserId).collect(Collectors.toSet());
        List<UserInfo> userInfoList = new ArrayList<>();
        if(fanIdSet.size() > 0){
            // 根据粉丝id集合获取粉丝用户信息userinfo
            userInfoList = userService.getUserInfoByUserIds(fanIdSet);
        }
        // 获取到该用户所关注的用户列表
        List<UserFollowing> followingList = userFollowingDao.getUserFollowings(userId);
        for(UserFollowing fan : fanList){
            for(UserInfo userInfo : userInfoList){
                // 所有的粉丝找到各自的用户信息, 并进行用户信息初始化, 为后面互关操作作准备
                if(fan.getUserId().equals(userInfo.getUserId())){
                    userInfo.setFollowed(false);
                    fan.setUserInfo(userInfo);
                }
            }
            for(UserFollowing following : followingList){
                // 在自己的关注列表和自己的粉丝中, 进行互关检查
                if(following.getFollowingId().equals(fan.getUserId())){
                    fan.getUserInfo().setFollowed(true);
                }
            }
        }
        return fanList;
    }

    // 添加用户关注分组
    public Long addUserFollowingGroups(FollowingGroup followingGroup) {
        // 设置创建时间
        followingGroup.setCreateTime(new Date());
        // 设置用户分组的类型 : 3
        followingGroup.setType(UserConstant.USER_FOLLOWING_GROUP_TYPE_USER);

        followingGroupService.addFollowingGroup(followingGroup);
        return followingGroup.getId();
    }

    // 获取用户关注分组
    public List<FollowingGroup> getUserFollowingGroups(Long userId) {
        return followingGroupService.getUserFollowingGroups(userId);
    }

    /**
     *
     * @param userInfoList 用户列表
     */
    public List<UserInfo> checkFollowingStatus(List<UserInfo> userInfoList, Long userId) {
        // 根据用户id获取用户的关注列表
        List<UserFollowing> userFollowingList = userFollowingDao.getUserFollowings(userId);
        // 遍历用户列表
        for(UserInfo userInfo : userInfoList){
            userInfo.setFollowed(false);
            // 编译用户关注列表
            for(UserFollowing userFollowing : userFollowingList){
                if(userFollowing.getFollowingId().equals(userInfo.getUserId())){
                    // 关注列表的关注用户id等于该用户id时, 关注的就是这个用户, 设置关注followed=true
                    userInfo.setFollowed(true);
                }
            }
        }
        return userInfoList;
    }
}